#include"DEMOTank_Base.h"

const float DEMOTank_Base::renderHeartBeatRate = 0.05f;
const float DEMOTank_Base::logicHeartBeatRate = 0.01f;

DEMOTank_Base::DEMOTank_Base()
{
	_renderComp = new DEMORenderComponent();

	_frameIdx = 0;

	_xScreen = _yScreen = 300;
	_rot = 0.0f;
	_scale = 1.0f;
	_col = 0xFFFFFFFF;

	
	resetRenderFrame( 0,1);
	resetSpeed( 0.0f,0.0f );
	resetState(ETS_Idle);
}

DEMOTank_Base::DEMOTank_Base(char* texName,char* sprDataName)
{
	_renderComp = new DEMORenderComponent();
}

DEMOTank_Base::~DEMOTank_Base()
{
	if(_renderComp)
	{
		_renderComp->destroyRes();

		delete _renderComp;
		_renderComp = NULL;
	}
}


void DEMOTank_Base::resetRenderFrame(int frameIdxIdx,int frameMovingIdx)
{
	_frameIdleIdx = frameIdxIdx;
	_frameMovingIdx = frameMovingIdx;
}

void DEMOTank_Base::resetSpeed( float vx,float vy)
{
	_speed.x = vx;
	_speed.y = vy;
}

void DEMOTank_Base::resetState( ETankState targetState)
{
	_curState = targetState;
}

void DEMOTank_Base::doMove()
{
	_xScreen += _speed.x;
	_yScreen += _speed.y;
}

void DEMOTank_Base::tick(float deltaTime)
{
	DEMO_Actor::tick(deltaTime);

	if( _renderComp != NULL)
	{
		_renderComp->setRenderParam( _frameIdx,_xScreen,_yScreen,_rot,_scale,_col,false);
	}


	ETankState targetState = (_speed.Length() == 0) ? ETS_Idle:ETS_Moving;
	resetState( targetState );
	/*if( targetState == ETS_Moving)
	{
		doMove();
	}*/
	
}

void DEMOTank_Base::OnHeartBeat_Render()
{
	if( _curState == ETS_Moving)
	{
		_frameIdx = (_frameIdx == 0)? _frameMovingIdx:_frameIdleIdx;
	}
	else
	{
		
	}	
}

void DEMOTank_Base::OnHeartBeat_Logic()
{
	if( _curState == ETS_Moving)
	{
		doMove();
	}
	else
	{
	
	}
	
}

void DEMOTank_Base::OnInput(hgeInputEvent theEvent)
{
	if( theEvent.type == INPUT_KEYDOWN )
	{
		switch(theEvent.key)
		{
		case HGEK_LEFT:

			moveToLeft();
			break;
		case HGEK_UP:

			moveToUp();
			break;
		case HGEK_RIGHT:

			moveToRight();
			break;
		case HGEK_DOWN:

			moveToDown();
			break;
		case HGEK_SPACE:
			resetSpeed( 0.0f,0.0f);
			break;
		default:

			break;
		}
	}

	if( theEvent.type == INPUT_KEYUP)
	{
		switch(theEvent.key)
		{
		case HGEK_LEFT:
		case HGEK_UP:
		case HGEK_RIGHT:
		case HGEK_DOWN:
			resetSpeed( 0.0f,0.0f);
			break;
		default:

			break;
		}
	}
}

void DEMOTank_Base::moveToUp()
{
	_speed.y = -1.0f;
	_speed.x = 0.0f;
	_rot = 0.0f;
}

void DEMOTank_Base::moveToDown()
{
	_speed.y = 1.0f;
	_speed.x = 0.0f;
	_rot = M_PI;
}

void DEMOTank_Base::moveToLeft()
{
	_speed.y = 0.0f;
	_speed.x = -1.0f;
	_rot = M_PI_2 + M_PI_2 + M_PI_2;
}

void DEMOTank_Base::moveToRight()
{
	_speed.y = 0.0f;
	_speed.x = 1.0f;
	_rot = M_PI_2;
}